﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    NotificationChannelUI.cs
//
// Abstract:
//    
//    Class for displaying a notification channel in the UI.
//
//--

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Media.Imaging;

namespace Microsoft.EnhancedReminders.OutlookAddIn
{
    /// <summary>
    /// Encapsulates a notification channel for the UI.
    /// </summary>
    public class NotificationChannelUI : INotifyPropertyChanged
    {
        private const int MaxDisplayNameLength = 30;

        private readonly NotificationChannel _channel;
        private readonly ChannelControl _channelControl;

        /// <summary>
        /// Constructor.
        /// </summary>
        public NotificationChannelUI(NotificationChannel channel, ChannelControl channelControl)
        {
            Debug.Assert(channel != null, "channel");
            Debug.Assert(channelControl != null, "channelControl");

            _channel = channel;
            _channelControl = channelControl;
        }

        /// <summary>
        /// The channel icon bitmap.
        /// </summary>
        public BitmapSource NotificationChannelIcon
        {
            get
            {
                Bitmap bitmap = null;

                switch (NotificationChannelType)
                {
                    case NotificationChannelType.Phone:
                        bitmap = Properties.Resources.Phone_large;
                        break;

                    case NotificationChannelType.Communicator:
                        bitmap = Properties.Resources.Im_large;
                        break;

                    case NotificationChannelType.SMS:
                        bitmap = Properties.Resources.SMS_large;
                        break;

                    default:
                        Debug.Fail("Unexpected channel type");
                        return null; // No icon is displayed in this case.
                }

                return WPFHelpers.ConvertToBitmapSource(bitmap);
            }
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// PropertyChanged event.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private void FirePropertyChangedEvent(string name)
        {
            Debug.Assert(!String.IsNullOrEmpty(name), "name");

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }

            _channelControl.FirePropertyChanged("Channels");
        }

        #endregion

        /// <summary>
        /// The globalized display string for the channel type.
        /// </summary>
        public string NotificationChannelTypeString
        {
            get
            {
                string channelType = null;

                switch (NotificationChannelType)
                {
                    case NotificationChannelType.Phone:
                        channelType = Properties.Resources.ChannelTypePhone;
                        break;

                    case NotificationChannelType.Communicator:
                        channelType = Properties.Resources.ChannelTypeCommunicator;
                        break;

                    case NotificationChannelType.SMS:
                        channelType = Properties.Resources.ChannelTypeSMS;
                        break;

                    default:
                        Debug.Fail("Unexpected channel type specified.");
                        return null;
                }

                return channelType;
            }
        }

        /// <summary>
        /// Gets/Sets the NotificationChannelType.
        /// </summary>
        public NotificationChannelType NotificationChannelType
        {
            get
            {
                return _channel.NotificationChannelType;
            }
            set // Update the channel type display string and icon.
            {
                Debug.Assert(_channel is NotificationCustomChannel, "Cannot change OCS channel type");

                _channel.NotificationChannelType = value;
                FirePropertyChangedEvent("NotificationChannelTypeString");
                FirePropertyChangedEvent("NotificationChannelIcon");
            }
        }

        /// <summary>
        /// Returns the (truncated if necessary) user-specified channel name.
        /// </summary>
        public string UserDefinedName
        {
            get
            {
                // Truncate the string to 30 characters.
                return WPFHelpers.TruncateString(_channel.UserDefinedName, MaxDisplayNameLength);
            }
            set
            {
                Debug.Assert(_channel is NotificationCustomChannel, "Cannot change OCS channel name");

                _channel.UserDefinedName = value;
                FirePropertyChangedEvent("UserDefinedName");
            }
        }

        /// <summary>
        /// IsEnabled property.  True to enable the channel for use.
        /// </summary>
        public bool IsEnabled
        {
            get
            {
                return _channel.IsEnabled;
            }
            set
            {
                _channel.IsEnabled = value;
                FirePropertyChangedEvent("IsEnabled");
                _channelControl.UpdateAllEnabled(); // Update the "enable all" checkbox.
            }
        }

        /// <summary>
        /// True if the checkbox bound to property "IsEnabled" should be enabled.
        /// </summary>
        public bool CheckBoxIsEnabled
        {
            get
            {
                return _channel is NotificationCustomChannel ||
                       !String.IsNullOrEmpty(_channel.UserDefinedValue);
            }
        }

        /// <summary>
        /// Notifies the UI that property CheckBoxIsEnabled may have changed.
        /// </summary>
        internal void RefreshCheckBoxIsEnabled()
        {
            FirePropertyChangedEvent("EnableCheckBox");
        }

        /// <summary>
        /// The notification channel.
        /// </summary>
        public NotificationChannel Channel
        {
            get
            {
                return _channel;
            }
        }
    }

    /// <summary>
    /// NotificationChannelUI collection data type which the ListBox binds to.
    /// </summary>
    public class NotificationChannelUICollection : ObservableCollection<NotificationChannelUI>
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public NotificationChannelUICollection()
        {
        }

        /// <summary>
        /// Converts the collection of NotificationChannelUI to a collection of NotificationChannel.
        /// </summary>
        internal ObservableCollection<NotificationChannel> Channels 
        {
            get
            {
                ObservableCollection<NotificationChannel> channels =
                    new ObservableCollection<NotificationChannel>();

                foreach (NotificationChannelUI uiChannel in Items)
                {
                    channels.Add(uiChannel.Channel.Copy());
                }

                return channels;
            }
        }
    }
}
